import 'dart:io';
import 'package:get/get.dart';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:homemedia/model/enum/device_type.dart';
import 'package:homemedia/model/enum/view_type.dart';
import 'package:homemedia/page/route_config.dart';
import 'package:homemedia/util/device_utils.dart';

class SplashLogic extends GetxController with StateMixin{
  late Rx<ViewType> viewType;
  late Rx<DeviceType> deviceType;
  late bool fadeOut = false;
  @override
  void onInit() {
    super.onInit();
    viewType = Rx(ViewType.unknow);
    deviceType = Rx(DeviceType.unknow);
    initData();
    toDeviceHome();
  }

  void initData() async {
    //判断当前的设备是什么类型
    if (Platform.isWindows) {
      DeviceUtils.viewType = ViewType.plus;
      DeviceUtils.deviceType = DeviceType.pc;
    } else if (Platform.isAndroid) {
      //安卓要区分 手机 / 平板 /电视
      DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
      AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;

      //bool isTV =
      //    androidInfo.systemFeatures.contains('android.software.leanback');

      // double sizeInches = androidInfo.displayMetrics.sizeInches;
      // double widthInches = androidInfo.displayMetrics.widthInches;
      if (androidInfo.displayMetrics.xDpi == 72 ||
          androidInfo.displayMetrics.yDpi == 72) {
        DeviceUtils.viewType = ViewType.phone;
        DeviceUtils.deviceType = DeviceType.phone; //模拟器
      } else if (androidInfo.displayMetrics.sizeInches < 7 &&
          !androidInfo.device.contains("emulator")) {
        DeviceUtils.viewType = ViewType.phone;
        DeviceUtils.deviceType = DeviceType.phone; //设备小于7寸 手机
      } else if (androidInfo.displayMetrics.sizeInches < 16 &&
          !androidInfo.device.contains("emulator")) {
        DeviceUtils.viewType = ViewType.plus;
        DeviceUtils.deviceType = DeviceType.tablet; //设备小于16寸 平板
      } else {
        DeviceUtils.viewType = ViewType.tv;
        DeviceUtils.deviceType = DeviceType.tv; //设备 电视 TV
      }
    }

    //赋值给当前页面
    viewType.value = DeviceUtils.viewType;
    deviceType.value = DeviceUtils.deviceType;
    change(null, status: RxStatus.success());
  }

  void toDeviceHome() async {
    // //读取保存的设备信息
    bool status = await DeviceUtils.loadDeviceConfig();
    // if (!status) {
    //   //读取不到代表首次启动
    //   await Get.to(() => const DeviceChosePage());
    // }

    //设置屏幕方向
    await DeviceUtils.setPreferredOrientations();

    //初始化 电视台列表
    // await TvProvider.init();

    await Future.delayed(const Duration(seconds: 1));

    fadeOut = true;

    await Future.delayed(const Duration(milliseconds: 500));

    //根据设备类型跳转不同的起始页面
    switch (DeviceUtils.viewType) {
    // case ViewType.phone:
    //   Get.off(() => const phone.PlayerPage(), transition: Transition.fadeIn);
    //   break;
    case ViewType.plus:
        Get.offAllNamed(RouteConfig.tvHome);
        break;
      case ViewType.tv:
      // Get.offAll(() => const TvHomePage(), transition: Transition.fadeIn);
        Get.offAllNamed(RouteConfig.tvHome);
        break;
      default:
    }
  }
}
